﻿/*
 * Created by SharpDevelop.
 * User: XPMUser
 * Date: 11/02/2011
 * Time: 12:31
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;

using Microsoft.Win32;

namespace KDG.FoldersSize4
{
	/// <summary>
	/// Description of SaveViewModel.
	/// </summary>
	public class SaveViewModel : ViewModelBase
	{
		private FoldersSizeModel _model { get; set; }
		public List<RelayNamedCommand> CommandCollection { get; private set; }
		public SaveViewModel()	
		{
			SaveToXmlCommand = new RelayNamedCommand( "Save to Xml",  p=>
					                                    {
			                                         		string fileName = RequestFileName(".xml");
			                                         		if (fileName != null) 
			                                         		{
				                                         		XDocument doc = new XDocument();
				                                         		
				                                         		doc.Add( new XElement("Root"));
				                                         		SaveXmlDocument(doc.Root, _model.RootItem);
				                                         		doc.Save(fileName);
				                                         		
				                                         		MessageBox.Show(string.Format("File save completed."));
			                                         		}
			                                         }, c => this.CanSave()
			                                        );
			
			SaveToHtmlCommand = new RelayNamedCommand( "Save to Html", p=>
					                                    {
			                                         	MessageBox.Show("SaveToHtmlCommand");
					                                    	
					                                    }, c => this.CanSave()
			                                        );
			SaveToTextCommand = new RelayNamedCommand( "Save to Txt", p=>
					                                    {
					                                    	string fileName = RequestFileName(".txt");
			                                         		if (fileName != null) 
			                                         		{
			                                         			using ( StreamWriter sw = File.CreateText(fileName) )
			                                         			{
			                                         				SaveTxtDocument(sw, _model.RootItem);
			                                         			}
				                                         		
				                                         		MessageBox.Show(string.Format("File save completed."));
			                                         		}
					                                    }, c => this.CanSave()
			                                        );
				
			CommandCollection = new List<RelayNamedCommand> 
			{
				SaveToXmlCommand, 
				SaveToHtmlCommand, 
				SaveToTextCommand
			};
		}
		bool CanSave()
		{
			return _model.RootItem!=null;
		}
		void SaveTxtDocument(StreamWriter sw, ScannerItem Item)
		{
			if(Item.IsDirectory)
				sw.WriteLine(string.Format("[{0}]", Item.Name));
			else
				sw.WriteLine(Item.Name);
			
			Item.Childs.ForEach( c => SaveTxtDocument(sw, c) );
		}
		void SaveXmlDocument(XElement doc, ScannerItem Item)
		{
			XElement xe = Item.IsDirectory?
				new XElement("folder", new XAttribute("path", Item.Name)):
				new XElement("file", new XAttribute("path", Item.Name));
			doc.Add( xe );
			
			Item.Childs.ForEach( c=>SaveXmlDocument(xe, c) );
		}
		string RequestFileName(string ext)
		{
			
			SaveFileDialog sfd = new SaveFileDialog()
			{
				CheckPathExists = true,
				DefaultExt = ext,
				OverwritePrompt = true,
				Filter = string.Format("Files {0}|*{0}", ext)
			};
			if(sfd.ShowDialog()==true)
				return sfd.FileName;
			
			return null;
		}
		public SaveViewModel(FoldersSizeModel Model) : this()
		{ 
			_model = Model; 
		}
		
		public RelayNamedCommand SaveToXmlCommand { get; private set; }
		public RelayNamedCommand SaveToHtmlCommand { get; private set; }
		public RelayNamedCommand SaveToTextCommand { get; private set; }
	}
}
